package org.ejc.api.convention;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
import org.ejc.api.transaction.TransactionAPILocal;
import org.ejc.api.transaction.TransactionAPIUtil;
import org.ejc.datatype.transaction.EJCTxSearchResult;
import org.ejc.persist.model.ConventionActivityHistory;
import org.ejc.util.EJCUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;


/**
 * @author zac
 * 
 * @ejb.bean type="Stateless" name="ConventionHistoryAPI"
 *           description="ConventionHistoryAPI session bean"
 *           local-jndi-name="org.ejc/ConventionHistoryAPILocalHome"
 *           jndi-name="org.ejc/ConventionHistoryAPIHome" view-type="both"
 * 
 * @ejb.ejb-ref ejb-name="ConventionHistoryAPI" ref-name="ejb/ConventionHistoryAPI"
 *              view-type="both"
 * 
 * @ejb.transaction type="RequiresNew"
 * @ejb.util generate = "physical"
 */
public abstract class ConventionHistoryAPIBean implements SessionBean{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ConventionHistoryAPIBean.class);
	
	
	/**
	 * 
	 * @ejb.interface-method view-type="both"
	 * 
	 */
	public Integer createConventionHistory(Integer conventionId,Date startDate,Date endDate) throws NamingException, EJBException {
	
		Session hsession = null;
		Transaction tx=null;
		Integer newId=null;
		try {
			hsession = EJCUtils.getHibernateSession();
			logger.debug("Got session now.....");
			ConventionActivityHistory historyObj=new ConventionActivityHistory();
			historyObj.setConventionId(conventionId);
			historyObj.setStartTime(new Timestamp(startDate.getTime()));
			historyObj.setEndTime(new Timestamp(endDate.getTime()));
			tx=hsession.beginTransaction();
			newId=(Integer)hsession.save(historyObj);
			if(logger.isDebugEnabled()){
				logger.debug("Created Convention Activity History with id "+newId);
			}
			tx.commit();
			hsession.flush();
		}catch(Exception e){
			logger.fatal("Error in creating convention history object !",e);
		}finally{
			if(hsession!=null){
				hsession.close();
			}
		}
		return null;
	}	
	
	/**
	 * 
	 * @ejb.interface-method view-type="both"
	 * 
	 */
	public List getConventionHistories(Integer conventionId)throws NamingException, EJBException {
		Session hsession = null;
		List returnList=new ArrayList();
		try {
			hsession = EJCUtils.getHibernateSession();
			Criteria crit=hsession.createCriteria(ConventionActivityHistory.class);
			crit.add(Expression.eq("conventionId", conventionId));
			crit.addOrder(Order.desc("endTime"));
			
			
			//returnList=crit.list();
			TransactionAPILocal localTx=TransactionAPIUtil.getLocalHome().create();
			
			for (Iterator iter = crit.list().iterator(); iter.hasNext();) {
				ConventionActivityHistory element = (ConventionActivityHistory) iter.next();
				EJCTxSearchResult result=localTx.getTxDetailsPerPeriod(element.getStartTime(), element.getEndTime(), conventionId);
				result.setTxHistoryId(element.getId());
				returnList.add(result);
			}
			
		
		
		}catch(Exception e){
			logger.fatal(e);
		}finally{
			hsession.close();
		}
		return returnList;
	}
	
	/**
	 * 
	 * @ejb.interface-method view-type="both"
	 * 
	 */
	public ConventionActivityHistory getConventionHistory(Integer conventionHistoryId){
		Session hsession = null;
		ConventionActivityHistory history=null;
		try {
			hsession = EJCUtils.getHibernateSession();
			history=(ConventionActivityHistory)hsession.load(ConventionActivityHistory.class,conventionHistoryId);
		}catch(Exception e){
			logger.fatal(e);
		}finally{
			hsession.close();
		}
		
		return history;
	}
	
	
	
	
	
}
